<template>
  <div>
    <top-bar>
      <template slot="left-icon">
        <van-icon name="arrow-left" size="20px" @click="goBack" />
      </template>
    </top-bar>
    <div class="video-box">
      <iframe :src="url" frameborder="0" width="98%" height="100%"></iframe>
    </div>
    <div class="video-content">
      <h2>{{ videoInfo.name }}</h2>
      <van-collapse v-model="activeNames">
        <van-collapse-item name="1">
          <template #title>
            <div class="left-text">
              <p>{{ videoInfo.score }}分</p>
              <p>·</p>
              <p>{{ videoInfo.free == 1 ? "免费" : "VIP" }}</p>
              <p>·</p>
              <p>{{ videoInfo.type }}</p>
            </div>
          </template>
          {{ videoInfo.description }}
        </van-collapse-item>
      </van-collapse>
      <div class="icon-box">
        <van-icon name="star-o" size="20px" />
        <van-icon name="cashier-o" size="20px" />
        <van-icon name="ellipsis" size="20px" />
      </div>
      <swiper class="swiper" :options="swiperOption">
        <swiper-slide
          :class="activeIndex == index ? 'swiper-active' : ''"
          v-for="(item, index) in videoList"
          :key="item.id"
          ><div
            style="width: 100%; height: 100%"
            @click="handleClick(item.url, index)"
          >
            {{ item.title }}
          </div></swiper-slide
        >
      </swiper>
      <recommend />
    </div>
  </div>
</template>

<script>
import detailsApi from "@/api/details/details";
import TopBar from "@/components/topbar";
import Recommend from "@/components/recommend";
export default {
  name: "details",
  components: {
    TopBar,
    Recommend,
  },
  data() {
    return {
      activeIndex: 0,
      videoId: 0,
      activeNames: [],
      swiperOption: {
        slidesPerView: 6,
        spaceBetween: 5,
        freeMode: true,
      },
      videoInfo: {},
      videoList: [],
      url: "",
    };
  },
  watch: {
    $route(newValue, oldValue) {
      this.videoId = this.$route.query.id;
      if (this.videoId) {
        this.getVideoInfo();
        this.getList();
      }
    },
  },
  created() {
    this.videoId = this.$route.query.id;
    if (this.videoId) {
      this.getVideoInfo();
      this.getList();
    }
  },
  methods: {
    async getVideoInfo() {
      let res = await detailsApi.getInfo(this.videoId);
      if (res.code === 20000) {
        this.videoInfo = res.data.data;
      } else {
        this.$toast(res.msg);
      }
    },
    async getList() {
      let res = await detailsApi.getVideos(this.videoId);
      if (res.code === 20000) {
        this.videoList = res.data.data;
        this.url = this.videoList[0].url;
      } else {
        this.$toast(res.msg);
      }
    },
    goBack() {
      this.$router.go("-1");
    },
    handleClick(url, index) {
      this.url = url;
      this.activeIndex = index;
    },
  },
};
</script>

<style lang="scss" scoped>
.video-box {
  margin-top: 54px;
  width: 100vw;
  height: 4.4rem;
  background: #000;
  display: flex;
  justify-content: center;
  align-items: center;
}
.video-content {
  h2,
  h3 {
    padding: 15px;
  }
  .left-text {
    display: flex;
    font-size: 14px;
    color: #666;
  }
  .icon-box {
    text-align: right;
    padding: 15px;
    .van-icon {
      margin-left: 20px;
    }
  }
  .swiper {
    width: 100%;
    height: 60px;
    padding: 0 15px;
    .swiper-slide {
      width: 50px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      background: #ccc;
    }
    .swiper-active {
      color: red;
    }
  }
}
</style>